package com.yuanjin.main;

import com.yuanjin.R;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity {
	private Fragment[] mFragments;
	private FragmentManager fragmentManager;
	private FragmentTransaction fragmentTransaction;
	private Button[] buttonGroup;
	private int currentTabIndex = 0;
	private LinearLayout ll;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.main);
		init();
		// FragmentIndicator();
	}

	private void init() {
		mFragments = new Fragment[5];
		fragmentManager = getSupportFragmentManager();
		// mFragments[0] = new FragmentMessage();
		mFragments[0] = new FriendsListFragment();
		mFragments[1] = new FragmentContact();
		// mFragments[1] = new Fragment_TulingChating2();
		mFragments[2] = new FragmentHome();
		mFragments[3] = new FragmentSchoolInOut();
		mFragments[4] = new FragmentMy();
		fragmentManager.beginTransaction()
				.add(R.id.fragment_container, mFragments[0]).commit();
		fragmentManager.beginTransaction()
				.add(R.id.fragment_container, mFragments[1]).commit();
		fragmentManager.beginTransaction()
				.add(R.id.fragment_container, mFragments[2]).commit();
		fragmentManager.beginTransaction()
				.add(R.id.fragment_container, mFragments[3]).commit();
		fragmentManager.beginTransaction()
				.add(R.id.fragment_container, mFragments[4]).commit();

		// 默认显示第一个
		fragmentTransaction = fragmentManager.beginTransaction()
				.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2])
				.hide(mFragments[3]).hide(mFragments[4]);
		fragmentTransaction.show(mFragments[0]).commit();

		// Button
		buttonGroup = new Button[5];
		buttonGroup[0] = (Button) findViewById(R.id.btn_message);
		buttonGroup[1] = (Button) findViewById(R.id.btn_contact);
		buttonGroup[2] = (Button) findViewById(R.id.btn_school);
		buttonGroup[3] = (Button) findViewById(R.id.btn_school_inout);
		buttonGroup[4] = (Button) findViewById(R.id.btn_setting);
		buttonGroup[0].setSelected(true);

		ll = (LinearLayout) findViewById(R.id.main_bottom);
	}

	// 页锟斤拷锟叫伙拷锟斤拷钮锟斤拷锟斤拷锟斤拷锟�
	@SuppressLint("NewApi")
	public void onTabSelect(View view) {
		// fragmentTransaction = fragmentManager.beginTransaction()
		// .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2])
		// .hide(mFragments[3]).hide(mFragments[4]);
		fragmentTransaction = fragmentManager.beginTransaction().hide(
				mFragments[currentTabIndex]);
		findViewById(view.getId()).setSelected(true);
		// 原锟饺帮拷钮锟戒暗
		buttonGroup[currentTabIndex].setSelected(false);
		switch (view.getId()) {
			case R.id.btn_message :
				fragmentTransaction.show(mFragments[0]).commit();

				currentTabIndex = 0;
				break;
			case R.id.btn_contact :

				fragmentTransaction.show(mFragments[1]).commit();
				currentTabIndex = 1;
				break;
			case R.id.btn_school :
				fragmentTransaction.show(mFragments[2]).commit();
				currentTabIndex = 2;
				break;
			case R.id.btn_school_inout :
				fragmentTransaction.show(mFragments[3]).commit();
				currentTabIndex = 3;
				break;
			case R.id.btn_setting :
				fragmentTransaction.show(mFragments[4]).commit();
				currentTabIndex = 4;
				break;

			default :
				break;
		}
		buttonGroup[currentTabIndex].setSelected(true);
	}

}